import 'package:app_30/home/provider/home_provider.dart';
import 'package:app_30/res/colors.dart';
import 'package:app_30/res/dimens.dart';
import 'package:app_30/util/toast_utils.dart';
import 'package:app_30/widgets/double_tap_back_exit_app.dart';
import 'package:app_30/widgets/load_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'fragment/home_page.dart';
import 'fragment/me_page.dart';
import 'fragment/share_page.dart';
import 'fragment/tx_page.dart';
import 'fragment/vip_page.dart';

class MyMainPage extends StatefulWidget {
  const MyMainPage({Key? key}) : super(key: key);

  @override
  State<MyMainPage> createState() => _MyMainPageState();
}

class _MyMainPageState extends State<MyMainPage> with RestorationMixin {
  static const double _imageSize = 25.0;
  int _selectedIndex = 0;
  final List<String> _appBarTitles = ['首页', 'VIP', '提现', '分享', '我的'];
  final PageController _pageController = PageController();
  List<BottomNavigationBarItem>? _list;
  late List<Widget> _pageList;

  HomeProvider provider = HomeProvider();

  @override
  void initState() {
    super.initState();
    initData();
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  void initData() {
    _pageList = [
      const HomePage(),
      const VipPage(),
      const TxPage(),
      const SharePage(),
      const MePage(),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<HomeProvider>(
      create: (_) => provider,
      child: DoubleTapBackExitApp(
        child: Scaffold(
            bottomNavigationBar: Consumer<HomeProvider>(
              builder: (_, provider, __) {
                return BottomNavigationBar(
                  items: _buildBottomNavigationBarItem(),
                  type: BottomNavigationBarType.fixed,
                  currentIndex: provider.value,
                  elevation: 5.0,
                  iconSize: 21.0,
                  selectedFontSize: Dimens.font_sp10,
                  unselectedFontSize: Dimens.font_sp10,
                  selectedItemColor: Theme.of(context).primaryColor,
                  unselectedItemColor: Colours.unselected_item_color,
                  onTap: (index) => _pageController.jumpToPage(index),
                );
              },
            ),
            // 使用PageView的原因参看 https://zhuanlan.zhihu.com/p/58582876
            body: PageView(
              physics: const NeverScrollableScrollPhysics(), // 禁止滑动
              controller: _pageController,
              onPageChanged: (int index) => provider.value = index,
              children: _pageList,
            )),
      ),
    );
  }

  List<BottomNavigationBarItem> _buildBottomNavigationBarItem() {
    if (_list == null) {
      const _tabImages = [
        [
          LoadAssetImage(
            'menu_home',
            width: _imageSize,
            color: Colours.dark_text_gray,
          ),
          LoadAssetImage(
            'menu_home',
            width: _imageSize,
            color: Colours.app_main,
          ),
        ],
        [
          LoadAssetImage(
            'menu_vip',
            width: _imageSize,
            color: Colours.dark_text_gray,
          ),
          LoadAssetImage(
            'menu_vip',
            width: _imageSize,
            color: Colours.app_main,
          ),
        ],
        [
          LoadAssetImage(
            'menu_tx',
            width: _imageSize,
            color: Colours.dark_text_gray,
          ),
          LoadAssetImage(
            'menu_tx',
            width: _imageSize,
            color: Colours.app_main,
          ),
        ],
        [
          LoadAssetImage(
            'menu_share',
            width: _imageSize,
            color: Colours.dark_text_gray,
          ),
          LoadAssetImage(
            'menu_share',
            width: _imageSize,
            color: Colours.app_main,
          ),
        ],
        [
          LoadAssetImage(
            'menu_my',
            width: _imageSize,
            color: Colours.dark_text_gray,
          ),
          LoadAssetImage(
            'menu_my',
            width: _imageSize,
            color: Colours.app_main,
          ),
        ]
      ];

      _list = List.generate(_tabImages.length, (i) {
        return BottomNavigationBarItem(
          icon: _tabImages[i][0],
          activeIcon: _tabImages[i][1],
          label: _appBarTitles[i],
        );
      });
    }
    return _list!;
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
      Toast.show(index.toString());
    });
  }

  void _onAdd() {}

  @override
  // TODO: implement restorationId
  String? get restorationId => 'home';

  @override
  void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
    registerForRestoration(provider, 'BottomNavigationBarCurrentIndex');
  }
}
